(function() {
  var ResponseDeleteConfirm,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  this.AdminResponseListingView = (function(_super) {

    __extends(AdminResponseListingView, _super);

    function AdminResponseListingView() {
      return AdminResponseListingView.__super__.constructor.apply(this, arguments);
    }

    AdminResponseListingView.prototype.el = $('#content');

    AdminResponseListingView.prototype.initialize = function() {
      AdminResponseListingView.__super__.initialize.apply(this, arguments);
      this.model.bind("change", this.render);
      return this.model.fetch();
    };

    AdminResponseListingView.prototype.render = function() {
      var el,
        _this = this;
      el = $(this.el);
      this.Navigate(this.model.url(), false, true);
      return this.GetTemplate(SiteMap.Admin.Responses.ListingTemplate, function(compiledTemplate) {
        el.html(compiledTemplate(_this.model.toJSON()));
        $('#Filter_UserEmail').autocomplete({
          source: SiteMap.Admin.Shared.GetUsers,
          minLength: 2,
          select: function(event, ui) {
            var filter;
            filter = _this.model.get("Filter");
            filter.UserId = ui.item.id;
            filter.UserEmail = ui.item.value;
            return _this.model.set("Filter", filter, {
              silent: true
            });
          }
        });
        $('#Filter_BookTitle').autocomplete({
          source: SiteMap.Admin.Shared.GetBooks,
          minLength: 2,
          select: function(event, ui) {
            var filter;
            filter = _this.model.get("Filter");
            filter.BookId = ui.item.id;
            filter.BookTitle = ui.item.value;
            return _this.model.set("Filter", filter, {
              silent: true
            });
          }
        });
        $("#Filter_DateFrom").datepicker();
        return $("#Filter_DateTo").datepicker();
      });
    };

    AdminResponseListingView.prototype.events = {
      "click .search": "search",
      "click .pagination a": "pagination",
      "click th>a": "sort",
      "click .buttons>.create": "create",
      "click .buttons>.delete": "remove",
      "click tbody tr>td:not(.chk)": "edit",
      "change .page-size": "changeSize",
      "click .reset": "reset"
    };

    AdminResponseListingView.prototype.search = function(event) {
      var filter;
      event.preventDefault();
      Loading.Start();
      Messages.WriteMessage('Apply search.');
      filter = this.model.get("Filter");
      filter.DateFrom = $("#Filter_DateFrom").val();
      filter.DateTo = $("#Filter_DateTo").val();
      if ($('#Filter_BookTitle').val() === '') {
        filter.BookId = 0;
        filter.BookTitle = '';
      }
      if ($('#Filter_UserEmail').val() === '') {
        filter.UserId = 0;
        filter.UserEmail = '';
      }
      this.model.set("Filter", filter, {
        silent: true
      });
      this.model.set("Page", 1, {
        silent: true
      });
      return this.model.fetch();
    };

    AdminResponseListingView.prototype.reset = function(event) {
      event.preventDefault();
      this.model.set("Filter", {
        UserId: 0,
        BookId: 0,
        UserEmail: '',
        BookTitle: '',
        DateFrom: '',
        DateTo: ''
      }, {
        silent: true
      });
      this.model.set("Page", 1, {
        silent: true
      });
      Loading.Start();
      return this.model.fetch();
    };

    AdminResponseListingView.prototype.remove = function(event) {
      var commentDeleteConfirm, form;
      event.preventDefault();
      form = $(this.el).find("form.listing");
      if (form.find("input:checked").length) {
        commentDeleteConfirm = new ResponseDeleteConfirm(this);
        return commentDeleteConfirm.Show("Are you sure?");
      } else {
        return Messages.WriteMessage('Choose one or more comments.');
      }
    };

    AdminResponseListingView.prototype.create = function(event) {
      event.preventDefault();
      Loading.Start();
      return this.Navigate(SiteMap.Admin.Responses.Create, true);
    };

    return AdminResponseListingView;

  })(ListingView);

  ResponseDeleteConfirm = (function(_super) {

    __extends(ResponseDeleteConfirm, _super);

    ResponseDeleteConfirm.view = null;

    function ResponseDeleteConfirm(view) {
      this.view = view;
    }

    ResponseDeleteConfirm.prototype.Ok = function() {
      var data, form,
        _this = this;
      form = $(this.view.el).find("form.listing");
      data = form.serialize();
      Loading.Start();
      Messages.WriteMessage('Deleting responses.');
      return $.post(SiteMap.Admin.Responses.Delete, data, function(response) {
        if (response.SuccessMessage !== null && response.SuccessMessage.length > 0) {
          Messages.WriteSuccessMessage(response.SuccessMessage);
          return _this.view.model.fetch();
        } else {
          Messages.WriteErrors(response.Errors);
          return Loading.Stop();
        }
      });
    };

    return ResponseDeleteConfirm;

  })(ConfirmDialog);

}).call(this);
